package com.hevery.crud.service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hevery.crud.bean.EasyUIResult;
import com.hevery.crud.mapper.UserMapper;
import com.hevery.crud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by zhy on 2017/8/23.
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public EasyUIResult queryUserList(Integer page,Integer rows){
        //设置分页参数
        PageHelper.startPage(page,rows);
        //查询user数据
        Example example = new Example(User.class);
        example.setOrderByClause("updated DESC");
//        List<User> users = userMapper.select(null);
        //设置排序条件
        List<User> users = userMapper.select(null);

        //获取分页后的信息
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        return new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 根据Id查询用户
     * @param userId
     * @return
     */
    public User queryUserById(Long userId){
        return userMapper.selectByPrimaryKey(userId);
    }
    /**
     * 新增用户
     * @param user
     * @return
     */
    public Boolean saveUser(User user){
        return userMapper.insert(user) == 1;
    }

    /**
     * 跟新用户
     * @param user
     * @return
     */
    public Boolean updateUser(User user){
        return userMapper.updateByPrimaryKeySelective(user) == 1;
    }
    public Boolean deleteUser(Long id){
        return  userMapper.deleteByPrimaryKey(id) == 1;
    }
}
